package com.dbs.config;

import javax.sql.DataSource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.env.Environment;

import com.alibaba.druid.pool.xa.DruidXADataSource;
import com.atomikos.jdbc.AtomikosDataSourceBean;

/**
   *  数据源1的配置
 * @author 34779
 *
 */
@Configuration
@MapperScan(basePackages = {"com.dbs.dao.test1"}, sqlSessionTemplateRef = "sqlSessionTemplateTest1") // 扫描dao或mapper接口
public class DataSourceTest1Config {

	@Autowired
    private Environment env;
	
    @Primary
    @Bean(name = "dataSourceTest1")
    public DataSource dataSourceCar(DataSourceTest1Properties dataSourceTest1Properties){
        DruidXADataSource dataSource = new DruidXADataSource();
        BeanUtils.copyProperties(dataSourceTest1Properties,dataSource);
        AtomikosDataSourceBean xaDataSource = new AtomikosDataSourceBean();
        xaDataSource.setXaDataSource(dataSource);
        xaDataSource.setUniqueResourceName("dataSourceTest1");
        return xaDataSource;
    }

//	@Primary
//	@Bean(name = "dataSourceTest1")
//    public DataSource myTestDbDataSource() throws Exception {
//        Properties props = new Properties();
//        props.put("driverClassName", env.getProperty("jdbc.driverClassName"));
//        props.put("url", env.getProperty("jdbc.url"));
//        props.put("username", env.getProperty("jdbc.username"));
//        props.put("password", env.getProperty("jdbc.password"));
//        AtomikosDataSourceBean xaDataSource = new AtomikosDataSourceBean();
//        DruidXADataSource dataSource = new DruidXADataSource();
//        BeanUtils.copyProperties(props,dataSource);
//        xaDataSource.setXaDataSource(dataSource);
//        xaDataSource.setUniqueResourceName("dataSourceTest1");
//        return xaDataSource;
//    }

    @Bean(name = "sqlSessionFactoryTest1")
    public SqlSessionFactory sqlSessionFactoryTest1(@Qualifier("dataSourceTest1") DataSource dataSource)
            throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        //bean.setTypeAliasesPackage("com.lcj.web.entity.car");
        //bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:/mapper/car/*Mapper.xml"));
        return bean.getObject();
    }

    @Bean(name = "sqlSessionTemplateTest1")
    public SqlSessionTemplate sqlSessionTemplateTest1(
            @Qualifier("sqlSessionFactoryTest1") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}

